package moodish.dummy.test;

import java.io.PrintStream;
import java.util.LinkedList;

import moodish.comm.ServerComm;
import moodish.comm.ServerSideMessage;

public class VirtualServerComm implements ServerComm {
	public enum testTypes {
		CONNECT, DISCONNECT, FRIENDSHIP, UNFRIENDSHIP, MOODISH
	};

	private LinkedList<String> onlineUsers = new LinkedList<String>();
	private LinkedList<ServerSideMessageTypes> commandQueue = new LinkedList<ServerSideMessageTypes>();
	private PrintStream printStream;

	public void setPrintStream(PrintStream printStream) {
		this.printStream = printStream;
	}

	public void defineType(testTypes testType) {
		switch (testType) {
		case CONNECT:
			commandQueue.add(ServerSideMessageTypes.CONNECT_CLIENT_1);
			commandQueue.add(ServerSideMessageTypes.CONNECT_CLIENT_2);
			break;
		case DISCONNECT:
			commandQueue.add(ServerSideMessageTypes.CONNECT_CLIENT_1);
			commandQueue.add(ServerSideMessageTypes.CONNECT_CLIENT_2);
			commandQueue.add(ServerSideMessageTypes.DISCONNECT_CLIENT_1);
			commandQueue.add(ServerSideMessageTypes.DISCONNECT_CLIENT_1);
			break;
		case FRIENDSHIP:
			commandQueue.add(ServerSideMessageTypes.CONNECT_CLIENT_1);
			commandQueue.add(ServerSideMessageTypes.CONNECT_CLIENT_2);
			commandQueue.add(ServerSideMessageTypes.FRIENDSHIP_1to2);
			commandQueue.add(ServerSideMessageTypes.FRIENDSHIP_1to2);
			commandQueue.add(ServerSideMessageTypes.FRIENDSHIP_1to3);
			commandQueue.add(ServerSideMessageTypes.FRIENDSHIP_3to1);
			break;
		case UNFRIENDSHIP:
			commandQueue.add(ServerSideMessageTypes.CONNECT_CLIENT_1);
			commandQueue.add(ServerSideMessageTypes.CONNECT_CLIENT_2);
			commandQueue.add(ServerSideMessageTypes.FRIENDSHIP_1to2);
			commandQueue.add(ServerSideMessageTypes.UNFRIENDSHIP_1to2);
			commandQueue.add(ServerSideMessageTypes.UNFRIENDSHIP_1to2);
			commandQueue.add(ServerSideMessageTypes.UNFRIENDSHIP_1to3);
			commandQueue.add(ServerSideMessageTypes.UNFRIENDSHIP_3to1);
			break;
		case MOODISH:
			commandQueue.add(ServerSideMessageTypes.CONNECT_CLIENT_1);
			commandQueue.add(ServerSideMessageTypes.CONNECT_CLIENT_2);
			commandQueue.add(ServerSideMessageTypes.FRIENDSHIP_1to2);
			commandQueue.add(ServerSideMessageTypes.MOODISH_1);
			commandQueue.add(ServerSideMessageTypes.MOODISH_1_BIG);
			commandQueue.add(ServerSideMessageTypes.MOODISH_3);
			break;
		}
	}

	@Override
	public void start() {
		printStream.print("[JUnit] Server Started\t");
	}

	@Override
	public boolean hasNextMessage() {
		return !commandQueue.isEmpty();
	}

	@Override
	public ServerSideMessage getNextMessage() {
		return commandQueue.pop().getMessage();
	}

	@Override
	public void sendNewFriendship(String toNickname, String newFriendship) {
		printStream.print("[JUnit] Friendship from " + newFriendship + " to "
				+ toNickname + "\t");
		System.out.println("[Virtual] NewFriendship");
	}

	@Override
	public void sendError(String toNickname, String error) {
		printStream.print("[JUnit] Sent to " + toNickname
				+ " the error message: \"" + error + "\"\t");
		System.out.println("[Virtual] Error");
	}

	@Override
	public boolean clientIsConnected(String nickname) {
		return onlineUsers.contains(nickname);
	}

	@Override
	public void sendClientConnected(String toNickname, String userConnected) {
		onlineUsers.add(userConnected);
		printStream.print("[JUnit] Notified " + toNickname + " that "
				+ userConnected + " connected\t");
		System.out.println("[Virtual] Connected");
	}

	@Override
	public void sendClientDisconnected(String toNickname,
			String userDisconnected) {
		onlineUsers.remove(userDisconnected);
		printStream.print("[JUnit] Notified " + toNickname + " that "
				+ userDisconnected + " disconnected\t");
		System.out.println("[Virtual] Disconnected");
	}

	@Override
	public void disconnectClient(String nickname) {
		onlineUsers.remove(nickname);
		printStream.print("[JUnit] Disconnected client " + nickname + "\t");
		System.out.println("[Virtual] Kill Client");
	}

	@Override
	public void sendMoodishMessage(String fromNicename, String toNickname,
			String message) {
		printStream.print("[JUnit] Server notified " + toNickname + " that "
				+ fromNicename + " mood is " + message + "\t");
		System.out.println("[Virtual] Moodish");
	}

	@Override
	public void sendNewUnfriendship(String toNickname, String newFriendship) {
		printStream.print("[JUnit] Unfriendship from " + newFriendship + " to "
				+ toNickname + "\t");
		System.out.println("[Virtual] Unfriendship");
	}
}
